package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016110300788916";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCJcPNA8Xvvc5g+11kTzSrSOz9jo6Lt7eCCqa49hWbJVFih7lStTW3j4XdATkDmqoR6vCdTn9ah3mxKpdOgPvNlLXFRkerExhzkgB8/xWa2wHqSGm/4eoZxA5VBYRFCjtT+XQQmDUZmF1yBCfYmpR6QOUFv+f87DC457XlxXX4WtuOMTLYY/fTmBf3MM2ulriuBKlPlbVs3lNn3fXVTRgfywf26W7OPNkpAXGvGvVHIz5LtYABXgvqBUHtQ35zY0oq4nukfTFsY9ab6fJYqX5dKvmJVYL8eu0SGruNo3JOAJ173e+u8yKIibUleflOgBomJvIICD8ZQUhkJiQwhPLs/AgMBAAECggEBAIJ0oZAdJqQsNnA/e5+d+G1AK3TqF3Z7ecTMRkTC4MNsugxJ6jns6seIdOam4FeRttnAyhtH9jQCqQtZxzutRN3eelcQh6Rq33XMXtzrBAmnPTB8w5c4xW90FTHPbtCcPnOJ2qw7oXy54+XfjLM1u4Lttb0RsD9d2mrFi6YdNddcMKg04B12cyXo3gs0dFLiq9QWYEHsR/7PZqKBjQF7Cw7vk2MtQwwo0lqIMveJ0qkM7gcZggffUlcfBd12szZKk8fEdc0rRHwDWSNF8qhbLDKHJetMMmpPxkVjyYtbBdtXp10JjwpfNuwF8fLxZ8qynmMNTXBGmolufAVx73wFMsECgYEAxVAbYjOukbNTmVPrcdbeQtAtHsBXp7Sbm0v0xIkdzh7z3ogeyRSDTpLAAvIZTRAbukLzi1gwufFTOJ1PXYuSAzZvIxDy9Kj4Dea0ApKBsC1x8sLQ296BJa5GktkThH7V9g/XNr25sWfwK4XLL83H1SgUzi9c27bsMU//a9qx3zECgYEAslIQSc2xoHdMwGPkQR8bX5sLj4AdQeHPd8zCtf7XfRKJfii+yZmW7kPHM/t2NJiSTT7XhH13yqfWGEDseDzfsdRYOrpd513LDn5/nqdwpV5jyNdN9flC0WXRtqJibMBjTOrmIfaL98TUyDXfx8KhLCXiiJjYUljl4O0tY4qRBW8CgYBzayiZiSHUmNGOH0jajpX0/1Fla53avAtKDGRhktOcfv6obXeUqCsnCzDGiFfEfGe7zO4p1KysGD5WH68NSVdgCbd5C7ES7MQtXYZu+sRiCjNdjhgehUmGgStHOzPvjUIbbai2nMTCwcn7q4fvrq/2QDvrYEkxPQ9syR7gOE1UAQKBgHYsCh1O0iL8TIHG8RHy8moGTr/3b+wkICBNMs/3JYj+St2vVNg3D52wjwe04l3yqinEtoBXwZYsaaZDCDKabMflZ97reKWGM7XUQPC8F9uPVONZQ5H5cL1iE3tL25wkVuo3VPE2+QTmjPhtM3KnWbXvGVParqjLvvOzWfv4bSjDAoGBAKJIl+Sq669hth9J3iBqjnW83BXjw4a0NBWH9svqwzc1tmaVWE4PhFMkGrqSQ94f77k4E/7EsUqoeKVF79fTVXwlvYByyq9LfFNihqpamuzbohhh2oDb1h30CLl885Q2mEdqSZENeiVuFoqIdWsDiY0xUBO4Vo/RCgJ5Nwxy3ADi";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsQhZ2/gr9yHDGNJ/f5yKNWU08dz0ZDDGbEtJDyTr6jWLcmKd7/Mk79k1elRdXDEAcgt2FuFFuy2LPGPXq5NZUC3GuenSojagO94dudWPCNDfJftbhI3Q+jxbtOfeF+mxhNnuQmaRPBL15IPimFs4y8IRgSMhb+w3rFot7Kbz6rtFVNavOaEtJqRYetM2vx6y7eHx9PuZxEFmus3VkP1PvSegwBI+NfGT7RYXBhrYXscNossnzZNw09o8sbLkyYG4Iuww6VO/gjTorIgNdYa/h5IgKne70/mZDBPQ+zjW3EUdknVEY0mW1H/egoAfP/jYSRCtUyo98Dkmin3nIFppYQIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://zbylb9q874.bjhttp.cn/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                +"\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
